﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using tenpay;

public partial class Payment_tenpay_index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //商户号
        string bargainor_id = "1206775501";

        //密钥
        string key = "c2fab55d3ac392fa7399376bf0c4ea61";

        //当前时间 yyyyMMdd
        string date = DateTime.Now.ToString("yyyyMMdd");

        //生成订单10位序列号，此处用时间和随机数生成，商户根据自己调整，保证唯一
        string strReq = "" + DateTime.Now.ToString("HHmmss") + TenpayUtil.BuildRandomStr(4);

        //商户订单号，不超过32位，财付通只做记录，不保证唯一性
        //string sp_billno = strReq;
        string total = "0";
        if (Request.QueryString["orderamount"] != null && Request.QueryString["orderamount"] != "")
        {
            total = Request.QueryString["orderamount"];

            try 
            {
                Convert.ToDecimal(total);
            }
            catch
            {
                total = "0";
            }
        }

        if (Request.QueryString["ordernum"] != null && Request.QueryString["ordernum"] != "")
        {
            strReq = Request.QueryString["ordernum"].Substring(Request.QueryString["ordernum"].Length-10);
        }
        string sp_billno = Request.QueryString["ordernum"];

        //财付通订单号，10位商户号+8位日期+10位序列号，需保证全局唯一
        string transaction_id = bargainor_id + date + strReq;
        string return_url = "http://www.51maihua.cn/Payment/tenpay/return_url.aspx";

        //创建PayRequestHandler实例
        PayRequestHandler reqHandler = new PayRequestHandler(Context);

        //设置密钥
        reqHandler.setKey(key);

        //初始化
        reqHandler.init();

        //-----------------------------
        //设置支付参数
        //-----------------------------
        reqHandler.setParameter("bargainor_id", bargainor_id);			//商户号
        reqHandler.setParameter("sp_billno", sp_billno);				//商家订单号
        reqHandler.setParameter("transaction_id", transaction_id);		//财付通交易单号
        reqHandler.setParameter("return_url", return_url);				//支付通知url
        reqHandler.setParameter("desc", "订单号：" + transaction_id);	//商品名称
        //reqHandler.setParameter("total_fee", "1");						//商品金额,以分为单位
        reqHandler.setParameter("total_fee", total);						//商品金额,以分为单位




        //用户ip,测试环境时不要加这个ip参数，正式环境再加此参数
        //reqHandler.setParameter("spbill_create_ip",Page.Request.UserHostAddress);

        //获取请求带参数的url
        string requestUrl = reqHandler.getRequestURL();

        string a_link = "<a target=\"_blank\" href=\"" + requestUrl + "\">" + "财付通支付" + "</a>";

        Response.Redirect(requestUrl);

        //Response.Write(a_link);


        //post实现方式
        /*
        reqHandler.getRequestURL();
        Response.Write("<form method=\"post\" action=\""+ reqHandler.getGateUrl() + "\" >\n");
        Hashtable ht = reqHandler.getAllParameters();
        foreach(DictionaryEntry de in ht) 
        {
            Response.Write("<input type=\"hidden\" name=\"" + de.Key + "\" value=\"" + de.Value + "\" >\n");
        }
        Response.Write("<input type=\"submit\" value=\"财付通支付\" >\n</form>\n");
        */

        //获取debug信息
        //string debuginfo = reqHandler.getDebugInfo();
        //Response.Write("<br/>" + debuginfo + "<br/>");

        //重定向到财付通支付
        //reqHandler.doSend();

    }
}
